import { materialUrl } from "@/util/util";
import store from "@/store/store";

const audioContext = uni.createInnerAudioContext();
audioContext.src = materialUrl("bg_music.mp3");
audioContext.autoplay = false;
audioContext.loop = true;
type TPlayStatus = "paused" | "playing";
function switchPlayStatus(status?: TPlayStatus) {
  const { gameState } = store;
  const currentSrc = materialUrl("bg_music.mp3");
  if (audioContext.src !== currentSrc) audioContext.src = currentSrc;
  switch (status) {
    case "paused": {
      audioContext.pause();
      gameState.audioPlaying = false;
      break;
    }
    case "playing": {
      audioContext.play();
      gameState.audioPlaying = true;
      break;
    }
    default: {
      const playing = gameState.audioPlaying;
      playing ? audioContext.pause() : audioContext.play();
      gameState.audioPlaying = !playing;
      gameState.audioClosed = playing;
      break;
    }
  }
}

export { switchPlayStatus };
